package com.xj.community.controller;

import com.xj.community.Service.CommentService;
import com.xj.community.entity.Comment;
import com.xj.community.entity.User;
import com.xj.community.util.MyThreadLocal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Date;

@Controller
@RequestMapping(path = "/comment")
public class CommentController {
    @Autowired
    private CommentService commentService;

    @RequestMapping(path = "/add/{discussPostId}",method = RequestMethod.POST)
    //注意这里是post请求，所以comment接收表单数据
    public String addComment(@PathVariable("discussPostId") int discussPostId, Comment comment){
        User user = MyThreadLocal.getUser();
        comment.setUserId(user.getId());
        comment.setCreateTime(new Date());
        comment.setStatus(0);
        commentService.insertComment(comment);
        return "redirect:/discuss/detail/" + discussPostId;//重定向，不是请求转发到项目中的另一个页面
    }
}
